package com.donglan.pethome.organization.controller;

import com.donglan.pethome.basic.util.AjaxResult;
import com.donglan.pethome.basic.util.FastDfsApiOpr;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


/**
 * @Author: zhangdonghui
 * @Date: 2022/2/9 20:45
 */
@RestController
@RequestMapping("/fastDfs")
public class FastdfsController {

    @PostMapping
    public AjaxResult upload(MultipartFile file){
        try {
            //获取文件名        aa.jpg
            String filename = file.getOriginalFilename();
            //获取文件后缀名  jpg
            String extension = FilenameUtils.getExtension(filename);
            //上传附件(返回上传路径)
            String upload = FastDfsApiOpr.upload(file.getBytes(), extension);
            return new AjaxResult().setResultObj(upload);
        } catch (IOException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 参数：完整路径 /goup1/xxxxx/yyyy
     * 返回值：成功与否，还要返回地址
     *
     */
    @DeleteMapping
    public AjaxResult del(@RequestParam(required = true,value = "path") String path){

        try{
            String pathTmp = path.substring(1); // goup1/xxxxx/yyyy
            String groupName =  pathTmp.substring(0, pathTmp.indexOf("/")); //goup1
            String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);// /xxxxx/yyyy
            System.out.println(groupName);
            System.out.println(remotePath);
            FastDfsApiOpr.delete(groupName, remotePath);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

}



